package com.then.basic.socket.core;

import android.support.annotation.NonNull;
import android.util.Log;

import com.then.basic.socket.SocketConfig;
import com.then.basic.socket.data.ProtocolCode;
import com.then.basic.socket.utils.I366PickUtil;

import timber.log.Timber;

/**
 * Created by 42524 on 2018/1/16.
 */

public class PokerReq {


    /**
     * 发送玩家操作
     *
     * @param action   1:下注  2:跟注  3:加注  4:全下 5:让牌  6:弃牌
     * @param roomPath
     * @param roomid
     * @param chips
     */
    public static void reqSendPlayerAction(int action, int roomPath, int roomid, int chips) {
        // TODO: 2018/1/16
        if (SocketConfig.getGameServer() != null) {
            Registration reg = prepareGameServer();
            if (!reg.socketIsConnect()) {
                Timber.e("roomEnableTakeInControl is not socketIsConnect");
                return;
            }
            if (reg.socketIsConnect()) {
                Object[][] objs = {
                        {60, action, I366PickUtil.TYPE_INT_1},//1:下注  2:跟注  3:加注  4:全下 5:让牌  6:弃牌 7超时
                        {132, roomid, I366PickUtil.TYPE_INT_4},//房间id
                        {131, roomPath, I366PickUtil.TYPE_INT_4},//房间路径
                        {133, chips, I366PickUtil.TYPE_INT_4}//下注积分数
                };
                // TODO: 2018/1/16
                byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_SEND_ACTION, SocketConfig.getUserId());
                reg.publishByte(bytes);
            }
        }
    }


    public static void pauseGame(int iRoomID, int iRoomPath, int iPause) {

        if (SocketConfig.getGameServer() != null) {
            Registration reg = prepareGameServer();
            if (!reg.socketIsConnect()) {
                Timber.e("roomEnableTakeInControl is not socketIsConnect");
                return;
            }
            Object[][] objs = {
                    {60, iPause, I366PickUtil.TYPE_INT_1},//0:continue 1:pause
                    {130, iRoomID, I366PickUtil.TYPE_INT_4},//roomID
                    {131, iRoomPath, I366PickUtil.TYPE_INT_4},//roomPath
            };
            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_PAUSE_GAME, SocketConfig.getUserId());
            reg.publishByte(bytes);
        }
    }


    /**
     * 亮牌操作
     */
    public static void showDown(int iRoomID, int iRoomPath, int pokernum, int isShow) {
        Registration reg = prepareGameServer();
        if (!reg.socketIsConnect()) {
            Timber.e("roomEnableTakeInControl is not socketIsConnect");
            return;
        }
        Object[][] objs = {
                {131, iRoomID, I366PickUtil.TYPE_INT_4},//roomID
                {132, iRoomPath, I366PickUtil.TYPE_INT_4},//roomPath
                {133, pokernum, I366PickUtil.TYPE_INT_4},//pokernum
                {134, isShow, I366PickUtil.TYPE_INT_4}
        };

        byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_SHOWDOWN, SocketConfig.getUserId());

        reg.publishByte(bytes);
    }


    /**
     * 请求坐下、站起 、离开房间
     *
     * @param action        1:坐下 2:站起 3:离开房间
     * @param seatId        座位id
     * @param roomid        房间id
     * @param roompath      房间路径
     * @param isCloseSocket 是否发送离开请求后立刻关闭socket
     */

    public static void reqSeatAction(int action, int seatId, int roomid, int roompath,
                                     String longitude, String latitude, boolean isCloseSocket) {
        Registration reg = prepareGameServer();
        if (!reg.socketIsConnect()) {
            return;
        }
        Object[][] objs = {
                {60, action, I366PickUtil.TYPE_INT_1}, // 1:坐下 2:站起 3:离开房间
                {61, seatId, I366PickUtil.TYPE_INT_1}, // 座位id
                {62, longitude, I366PickUtil.TYPE_STRING_UTF16},   // 经度
                {63, latitude, I366PickUtil.TYPE_STRING_UTF16},    // 纬度
                {130, roompath, I366PickUtil.TYPE_INT_4}, // 房间路径
                {131, roomid, I366PickUtil.TYPE_INT_4} // 房间id
        };
        byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_SEAT_ACTION, SocketConfig.getUserId());
        reg.publishByte(bytes);
        if (isCloseSocket) {
            closeGameServer();
        }
    }


    /************************ROOM SERVICE***************************/
    /**
     * 请求 进入房间
     *
     * @param roomid
     * @param roompath
     * @param type          0: 快速 1:自选座位
     * @param reconnectFlag 是否断线重连0:是 1:不是
     */
    public static void reqEnterRoom(int roomid, int roompath, int type, int reconnectFlag, int clubId) {
        Registration reg = prepareGameServer();
        if (reg.socketIsConnect()) {
            Timber.d("gameserver socketIsConnect");
            Object[][] objs = {{131, roompath, I366PickUtil.TYPE_INT_4},
                    {132, roomid, I366PickUtil.TYPE_INT_4},
                    {133, clubId, I366PickUtil.TYPE_INT_4},
                    {60, type, I366PickUtil.TYPE_INT_1},
                    {61, 1, I366PickUtil.TYPE_INT_1} // 重连标志：不要使用0，服务器有问题
            };

            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_ENTER_ROOM, SocketConfig.getUserId());
            reg.publishByte(bytes);
        } else {
            Timber.d("gameserver is not socketIsConnect");
        }
    }


    /**
     * 房主请求开始游戏
     *
     * @param roomPath
     * @param iRoomID
     */
    public static void roomStartGame(int roomPath, int iRoomID) {

        if (SocketConfig.getGameServer() != null) {
            Registration reg = prepareGameServer();
            if (!reg.socketIsConnect()) {
                Timber.e("roomStartGame is not socketIsConnect");
                return;
            }
            Object[][] objs = {
                    {130, roomPath, I366PickUtil.TYPE_INT_4},//房间路径
                    {131, iRoomID, I366PickUtil.TYPE_INT_4}//房间编号
            };
            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_ROOM_START_GAME, SocketConfig.getUserId());
            reg.publishByte(bytes);
        }
    }


    public static void roomEnableTakeInControl(int roomPath, int iRoomID, boolean bEnable) {

        if (SocketConfig.getGameServer() != null) {
            Registration reg = prepareGameServer();
            if (!reg.socketIsConnect()) {
                Timber.e("roomEnableTakeInControl is not socketIsConnect");
                return;
            }

            Object[][] objs = {
                    {60, bEnable ? 1 : 0, I366PickUtil.TYPE_INT_1},//1:开启 0:关闭
                    {130, roomPath, I366PickUtil.TYPE_INT_4},//房间路径
                    {131, iRoomID, I366PickUtil.TYPE_INT_4},//房间编号
            };

            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_ROOM_TAKE_IN_CONTROL_SWITCH, SocketConfig.getUserId());

            reg.publishByte(bytes);
        }
    }


    public static void roomChangeTakeInRange(int iRoomID, int iRoomPath, int minRate, int maxRate) {
        if (SocketConfig.getGameServer() != null) {
            Registration reg = prepareGameServer();
            if (!reg.socketIsConnect()) {
                Timber.e("roomEnableTakeInControl is not socketIsConnect");
                return;
            }

            Object[][] objs = {
                    {130, iRoomID, I366PickUtil.TYPE_INT_4},//房间编号
                    {131, iRoomPath, I366PickUtil.TYPE_INT_4},//房间Path
                    {132, minRate, I366PickUtil.TYPE_INT_4},//最小带入倍数
                    {133, maxRate, I366PickUtil.TYPE_INT_4},//最大带入倍数
            };
            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_ROOM_TAKE_IN_RANGE_CHANGE, SocketConfig.getUserId());
            reg.publishByte(bytes);
        }
    }


    public static void roomPermitUserTakeIn(int roomPath, int iRoomID, int uid, int chips) {
        if (SocketConfig.getGameServer() != null) {
            Registration reg = prepareGameServer();
            if (!reg.socketIsConnect()) {
                Timber.e("roomEnableTakeInControl is not socketIsConnect");
                return;
            }

            Object[][] objs = {
                    {130, roomPath, I366PickUtil.TYPE_INT_4},//房间路径
                    {131, iRoomID, I366PickUtil.TYPE_INT_4},//房间编号
                    {132, uid, I366PickUtil.TYPE_INT_4}, //允许带入的用户ID
                    {133, chips, I366PickUtil.TYPE_INT_4},//允许用户带入积分数量，0代表拒绝了带入请求
            };
            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_ROOM_TAKE_IN_CONTROL_PERMIT, SocketConfig.getUserId());
            reg.publishByte(bytes);
        }
    }


    /**
     * 请求添加积分
     *
     * @param seatId
     * @param chips
     */
    public static void reqAddChips(int roomPath, int roomId, int seatId, int chips,
                                   int filledToLastHand, String longitude, String latitude) {
        Registration reg = prepareGameServer();
        if (!reg.socketIsConnect()) {
            return;
        }
        if (reg != null) {
            Object[][] objs = {
                    {60, seatId, I366PickUtil.TYPE_INT_1},//seatId
                    {62, longitude, I366PickUtil.TYPE_STRING_UTF16},   // 经度
                    {63, latitude, I366PickUtil.TYPE_STRING_UTF16},    // 纬度
                    {130, chips, I366PickUtil.TYPE_INT_4},//chips
                    {131, roomPath, I366PickUtil.TYPE_INT_4}, // 房间路径
                    {132, roomId, I366PickUtil.TYPE_INT_4},    // 房间ID
                    {133, filledToLastHand, I366PickUtil.TYPE_INT_4}, // 是否是补齐上次离开的带入, 1:是 0:不是
            };
            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_ADD_CHIPS, SocketConfig.getUserId());
            reg.publishByte(bytes);
        }
    }


    public static void addTime(int iRoomID, int iRoomPath, int iTime, int chips, int iSeatId, boolean insuranceMode) {
        if (SocketConfig.getGameServer() != null) {
            Registration reg = prepareGameServer();
            if (reg == null) {
                return;
            }
            if (!reg.socketIsConnect()) {
                Timber.e("roomEnableTakeInControl is not socketIsConnect");
                return;
            }
            Object[][] objs = {
                    {130, iRoomID, I366PickUtil.TYPE_INT_4},//房间编号
                    {131, iRoomPath, I366PickUtil.TYPE_INT_4},//房间Path
                    {132, iTime, I366PickUtil.TYPE_INT_4},//延时时间(秒)
                    // 133 是扣钻石，不要占用
                    {134, iSeatId, I366PickUtil.TYPE_INT_4},//座位号
                    {135, chips, I366PickUtil.TYPE_INT_4},//消耗积分
            };
            byte[] bytes = I366PickUtil.packAll(objs,
                    insuranceMode ? ProtocolCode.REQ_INSURANCE_ADD_TIME : ProtocolCode.REQ_ADD_TIME,
                    SocketConfig.getUserId());
            reg.publishByte(bytes);
        }
    }

    /**
     * 请求 进入MTT房间
     */
    public static void reqMttEnterRoom(int roomid, int roompath, int realRoomId) {
        Registration reg = prepareGameServer();
        if (reg.socketIsConnect()) {
            Timber.d("reqMttEnterRoom socketIsConnect");
            Object[][] objs = {
                    {131, roompath, I366PickUtil.TYPE_INT_4},
                    {132, roomid, I366PickUtil.TYPE_INT_4},
                    {133, realRoomId, I366PickUtil.TYPE_INT_4}
            };

            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_MTT_ENTER_ROOM, SocketConfig.getUserId());
            reg.publishByte(bytes);
        } else {
            Timber.d("reqMttEnterRoom is not socketIsConnect");
        }
    }


    // 一次获取牌桌中MTT所有参赛者列表
    public static void getMttAllPeople(int iRoomID, int iRoomPath) {
        if (SocketConfig.getGameServer() != null) {
            Registration reg = prepareGameServer();
            if (!reg.socketIsConnect()) {
                Timber.e("getMttAllPeople is not socketIsConnect");
                return;
            }
            Object[][] objs = {
                    {130, iRoomID, I366PickUtil.TYPE_INT_4},//房间编号
                    {131, iRoomPath, I366PickUtil.TYPE_INT_4},//房间Path
            };
            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_MTT_ALL_PEOPLE, SocketConfig.getUserId());
            reg.publishByte(bytes);
        }
    }


    /**
     * 请求 进入MTT房间
     */
    public static void reqSngEnterRoom(int roomid, int roompath) {
        Registration reg = prepareGameServer();
        if (reg.socketIsConnect()) {
            Timber.d("reqSngEnterRoom socketIsConnect");
            Object[][] objs = {
                    {131, roompath, I366PickUtil.TYPE_INT_4},
                    {132, roomid, I366PickUtil.TYPE_INT_4},
            };

            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_SNG_ENTER_ROOM, SocketConfig.getUserId());
            reg.publishByte(bytes);
        } else {
            Timber.d("reqSngEnterRoom is not socketIsConnect");
        }
    }


    /**
     * 请求牌局延时
     */
    public static void addSngTime(int iRoomID, int iRoomPath, int iTime, int chips, int iSeatId) {
        if (SocketConfig.getGameServer() != null) {
            Registration reg = prepareGameServer();
            if (reg == null) {
                return;
            }
            if (!reg.socketIsConnect()) {
                Timber.e("addSngTime is not socketIsConnect");
                return;
            }
            Object[][] objs = {
                    {130, iRoomID, I366PickUtil.TYPE_INT_4}, // 房间编号
                    {131, iRoomPath, I366PickUtil.TYPE_INT_4}, // 房间Path
                    {132, iTime, I366PickUtil.TYPE_INT_4}, // 延时时间(秒)
                    // 133 是扣钻石，不要占用
                    {134, iSeatId, I366PickUtil.TYPE_INT_4}, // 座位号
                    {135, chips, I366PickUtil.TYPE_INT_4}, // 消耗积分
            };
            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_SNG_ADD_TIME, SocketConfig.getUserId());
            reg.publishByte(bytes);
        }
    }


    /**
     * omaha请求 进入房间
     *
     * @param roomid
     * @param roompath
     * @param type     0: 快速 1:自选座位
     */
    public static void reqEnterRoomOmaha(int roomid, int roompath, int type) {
        Registration reg = prepareGameServer();
        if (reg.socketIsConnect()) {
            Timber.d("reqEnterRoomOmaha socketIsConnect");
            Object[][] objs = {{131, roompath, I366PickUtil.TYPE_INT_4},
                    {132, roomid, I366PickUtil.TYPE_INT_4},
                    {60, type, I366PickUtil.TYPE_INT_1},
                    {61, 1, I366PickUtil.TYPE_INT_1} // 重连标志：不要使用0，服务器有问题
            };

            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_OMAHA_ENTER_ROOM, SocketConfig.getUserId());
            reg.publishByte(bytes);
        } else {
            Timber.d("reqEnterRoomOmaha is not socketIsConnect");
        }
    }


    /**
     * 大菠萝请求 进入房间
     *
     * @param roomid
     * @param roompath
     * @param type     0: 快速 1:自选座位
     */
    public static void reqEnterRoomPineapple(int roomid, int roompath, int type) {
        Registration reg = prepareGameServer();
        if (reg.socketIsConnect()) {
            Timber.d("reqEnterRoomPineapple socketIsConnect");
            Object[][] objs = {{131, roompath, I366PickUtil.TYPE_INT_4},
                    {132, roomid, I366PickUtil.TYPE_INT_4},
                    {60, type, I366PickUtil.TYPE_INT_1},
                    {61, 1, I366PickUtil.TYPE_INT_1} // 重连标志：不要使用0，服务器有问题
            };
            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_PINEAPPLE_ENTER_ROOM, SocketConfig.getUserId());
            reg.publishByte(bytes);
        } else {
            Timber.d("reqEnterRoomOmaha is not socketIsConnect");
        }
    }

    // 请求离桌留座
    public static void reqKeepSeat(int iRoomID, int iRoomPath, byte requestCode, int minutes, int versionCode) {
        if (SocketConfig.getGameServer() != null) {
            Registration reg = prepareGameServer();
            if (!reg.socketIsConnect()) {
                Timber.e("reqDismissMessage is not socketIsConnect");
                return;
            }
            Object[][] objs = {
                    {60, requestCode, I366PickUtil.TYPE_INT_1}, // 1主动请求，2查询自己的状态
                    {131, iRoomPath, I366PickUtil.TYPE_INT_4}, // roomPath
                    {132, iRoomID, I366PickUtil.TYPE_INT_4},  // roomID
                    {133, minutes, I366PickUtil.TYPE_INT_4},  // minutes
                    {134, versionCode, I366PickUtil.TYPE_INT_4}//标示版本号
            };

            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_KEEP_SEAT, SocketConfig.getUserId());

            reg.publishByte(bytes);
        }
    }

    // 请求发下一轮牌
    public static void reqNextPublicCard(int iRoomID, int iRoomPath, byte publicCardCount) {
        if (SocketConfig.getGameServer() != null) {
            Registration reg = prepareGameServer();
            if (!reg.socketIsConnect()) {
                Timber.e("reqDismissMessage is not socketIsConnect");
                return;
            }
            Object[][] objs = {
                    {60, publicCardCount, I366PickUtil.TYPE_INT_1}, // 公共牌张数
                    {131, iRoomPath, I366PickUtil.TYPE_INT_4}, // roomPath
                    {132, iRoomID, I366PickUtil.TYPE_INT_4},  // roomID
            };
            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_PAY_NEXT_PUBLIC_CARD, SocketConfig.getUserId());
            reg.publishByte(bytes);
        }
    }


    // 请求延长牌桌时间
    public static void reqDelayRoomTime(int iRoomID, int iRoomPath, int roomDelayMinutes) {
        if (SocketConfig.getGameServer() != null) {
        Registration reg = prepareGameServer();
        if (!reg.socketIsConnect()) {
            Timber.e("reqDismissMessage is not socketIsConnect");
            return;
        }
        Object[][] objs = {
                {131, iRoomID, I366PickUtil.TYPE_INT_4}, // roomPath
                {132, iRoomPath, I366PickUtil.TYPE_INT_4},  // roomID
                {133, roomDelayMinutes, I366PickUtil.TYPE_INT_4}, // 延长的时间（分钟）
        };
            byte[] bytes = I366PickUtil.packAll(objs, ProtocolCode.REQ_DELAY_ROOM_TIME, SocketConfig.getUserId());

        reg.publishByte(bytes);
        }
    }

    public static byte[] getCheckVersionReq() {
        return I366PickUtil.packAll(new Object[][]{}, ProtocolCode.REQ_CHECK_VERSION, SocketConfig.getUserId());
    }


    @NonNull
    public static Registration prepareGameServer() {
        synchronized (Registration.class) {
            if (SocketConfig.getGameServer() == null) {
                Timber.e("prepareGameServer: getGame is null");
                return null;
            }
            Registration reg = Registration.getYetReg(SocketConfig.getGameServer());
            if (reg != null) {
                if (reg.socketIsConnect()) {
                    return reg;
                } else {
                    reg.close();
                    Registration.moueOutYetReg(SocketConfig.getGameServer());
                    reg = null;
                }
            }
            Timber.i("Create Game Server Socket!");
            reg = new Registration();
            reg.getLongReg(SocketConfig.getGameServer(), 2);
            return reg;
        }
    }

    public static void closeGameServer() {
        Timber.i("Close Game Server.");
        synchronized (Registration.class) {
            if (SocketConfig.getGameServer() != null) {
                Registration reg = Registration.getYetReg(SocketConfig.getGameServer());
                if (reg != null) {
                    Timber.i("Game Server Closing.");
                    reg.close();
                    Registration.moueOutYetReg(SocketConfig.getGameServer());
                }
            }
        }
    }


    public static void closeResourceServer() {
        Timber.i("Close Resource Server");
        synchronized (Registration.class) {
            Object[] resConfig = SocketConfig.getResourceServer();
            if (resConfig != null) {
                Registration reg = Registration.getYetReg(SocketConfig.getResourceServer());
                if (reg != null) {
                    reg.close();
                    Registration.moueOutYetReg(SocketConfig.getResourceServer());
                }
            }
        }
    }

}
